package com.wnhz.work.yz.activity;

import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.adapter.AllProjectAdapter;
import com.wnhz.work.yz.bean.AllProjectBean;
import com.wnhz.work.yz.fragment.project.AccomplishFragment;
import com.wnhz.work.yz.fragment.project.AllFragment;
import com.wnhz.work.yz.fragment.project.AuditFragment;
import com.wnhz.work.yz.fragment.project.ProceedFragment;
import com.wnhz.work.yz.utils.AlbumImagePath;
import com.wnhz.work.yz.view.SkillBookPop;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class AllProjectActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private AllFragment fragment1;
    private AuditFragment fragment2;
    private ProceedFragment fragment3;
    private AccomplishFragment fragment4;

    private List<Fragment> fragmentList;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;

    private TextView text1, text2, text3, text4;
    private View line1, line2, line3, line4;

    private int textColorNor, textColorCli;

    private String type;

    private List<AllProjectBean> beanList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_project);

        initData();
        initView();
    }

    private String user_id;

    private void initData() {
        textColorNor = getResources().getColor(R.color.text_two);
        textColorCli = getResources().getColor(R.color.main_color);
        type = getIntent().getStringExtra("type");
        user_id = getIntent().getStringExtra("user_id");
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("项目");
        findViewById(R.id.btn_main_left).setOnClickListener(this);

        text1 = (TextView) findViewById(R.id.tv_all);
        text2 = (TextView) findViewById(R.id.tv_audit);
        text3 = (TextView) findViewById(R.id.tv_proceed);
        text4 = (TextView) findViewById(R.id.tv_accomplish);

        line1 = findViewById(R.id.line1);
        line2 = findViewById(R.id.line2);
        line3 = findViewById(R.id.line3);
        line4 = findViewById(R.id.line4);

        text1.setOnClickListener(this);
        text2.setOnClickListener(this);
        text3.setOnClickListener(this);
        text4.setOnClickListener(this);

        fragmentList = new ArrayList<>();
        fragment1 = new AllFragment();
        fragment1.setType("0");
        fragment1.setUser_id(user_id);
        fragment2 = new AuditFragment();
        fragment2.setType("1");
        fragment2.setUser_id(user_id);
        fragment3 = new ProceedFragment();
        fragment3.setType("2");
        fragment3.setUser_id(user_id);
        fragment4 = new AccomplishFragment();
        fragment4.setType("3");
        fragment4.setUser_id(user_id);

        fragmentList.add(fragment1);
        fragmentList.add(fragment2);
        fragmentList.add(fragment3);
        fragmentList.add(fragment4);

        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.fl_project, fragment1);
        transaction.add(R.id.fl_project, fragment2);
        transaction.add(R.id.fl_project, fragment3);
        transaction.add(R.id.fl_project, fragment4);

        transaction.hide(fragment2);
        transaction.hide(fragment3);
        transaction.hide(fragment4);
        transaction.commit();
        // showDialog();
        //  upload(type);

    }

    private static final String TAG = "AllProjectActivity";

//    public void upload(final String type) {
//        Map<String, Object> map = new HashMap<>();
//        map.put("token", MyApplication.getInstance().getUserBean().getToken());
//        map.put("type", type);
//        XUtil.Post(Url.BUDGET_GETBUDGETLIST, map, new MyCallBack<String>() {
//            @Override
//            public void onSuccess(String result) {
//                super.onSuccess(result);
//                LogUtil.e(TAG, "onSuccess:BUDGET_= " + result);
//                try {
//                    JSONObject jsonObject = new JSONObject(result);
//                    String res = jsonObject.optString("result");
//                    JSONArray jsonArray = jsonObject.optJSONArray("info");
//                    beanList.clear();
//                    if (jsonArray != null) {
//                        for (int i = 0; i < jsonArray.length(); i++) {
//                            AllProjectBean bean = new AllProjectBean();
//                            Gson gson = new Gson();
//                            bean = gson.fromJson(jsonArray.optString(i), AllProjectBean.class);
//                            beanList.add(bean);
//                        }
//                    }
//
//                    if ("0".equals(type)) {
//                        fragment1.setData(beanList);
//                    } else if ("1".equals(type)) {
//                        fragment2.setData(beanList);
//                    } else if ("2".equals(type)) {
//                        fragment3.setData(beanList);
//                    } else if ("3".equals(type)) {
//                        fragment4.setData(beanList);
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//            }
//
//            @Override
//            public void onFinished() {
//                closeDialog();
//                super.onFinished();
//            }
//
//            @Override
//            public void onError(Throwable ex, boolean isOnCallback) {
//                closeDialog();
//                super.onError(ex, isOnCallback);
//            }
//        });
//    }

    private void hideFragment(int index) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        for (int i = 0; i < fragmentList.size(); i++) {
            if (i == index) {
                transaction.show(fragmentList.get(i));
            } else {
                transaction.hide(fragmentList.get(i));
            }
        }
        transaction.commit();
    }

    @Override
    public void onClick(View v) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //全部
            case R.id.tv_all:
                hideFragment(0);
                transaction.show(fragmentList.get(0)).commit();
                text1.setTextColor(textColorCli);
                text2.setTextColor(textColorNor);
                text3.setTextColor(textColorNor);
                text4.setTextColor(textColorNor);

                line1.setVisibility(View.VISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.INVISIBLE);
                line4.setVisibility(View.INVISIBLE);

                //    upload("0");
                break;

            //审核中
            case R.id.tv_audit:
                hideFragment(1);
                transaction.show(fragmentList.get(1)).commit();
                text1.setTextColor(textColorNor);
                text2.setTextColor(textColorCli);
                text3.setTextColor(textColorNor);
                text4.setTextColor(textColorNor);

                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.VISIBLE);
                line3.setVisibility(View.INVISIBLE);
                line4.setVisibility(View.INVISIBLE);

                //    upload("1");
                break;

            //进行中
            case R.id.tv_proceed:
                hideFragment(2);
                transaction.show(fragmentList.get(2)).commit();
                text1.setTextColor(textColorNor);
                text2.setTextColor(textColorNor);
                text3.setTextColor(textColorCli);
                text4.setTextColor(textColorNor);

                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.VISIBLE);
                line4.setVisibility(View.INVISIBLE);

                //    upload("2");
                break;

            //已完成
            case R.id.tv_accomplish:
                hideFragment(3);
                transaction.show(fragmentList.get(3)).commit();
                text1.setTextColor(textColorNor);
                text2.setTextColor(textColorNor);
                text3.setTextColor(textColorNor);
                text4.setTextColor(textColorCli);

                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.INVISIBLE);
                line4.setVisibility(View.VISIBLE);

                //   upload("3");
                break;

            default:
                break;
        }
    }

    private WindowManager.LayoutParams lp;
    private SkillBookPop menuWindow;
    private final int FROM_CAMERA_CODE = 111;// 调用相机更改背景图片的请求code
    private final int FROM_ALBUM_CODE = 112;// 调用相册更改背景图片的请求code
    private File tempFile;
    private String imageName;// 原图名称
    private String imagePath;// 原图路径

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            //相册
            case FROM_ALBUM_CODE:
                Bitmap bm = null;
                ContentResolver resolver = getContentResolver();

                if (null != data) {
                    // 得到图片的全路径
                    Uri uri = data.getData();

                    String imagePath = AlbumImagePath.getPath(this, uri);
                    System.out.println("imagePath**====www==" + imagePath);

                    updateImage(imagePath, uri);

                }
                break;
            //拍照
            case FROM_CAMERA_CODE:
                boolean hasSdcard = hasSdcard();
                if (hasSdcard) {
                    SharedPreferences sp = getSharedPreferences("imageName.txt", MODE_PRIVATE);
                    imageName = sp.getString("imageName", "");
                    tempFile = new File(Environment.getExternalStorageDirectory() + "/youZhu/", imageName);
                    Uri uri = Uri.fromFile(tempFile);

                    String imagePath = tempFile.getAbsolutePath();
                    System.out.println("拍照的路径=====：" + imagePath);
                    File file = new File(imagePath);
                    if (!file.exists()) {
                        return;
                    }

                    updateImage(imagePath, uri);

                } else {
                    MyToast("未找到存储卡，无法存储照片！");
                }
                break;

        }
    }

    private boolean hasSdcard() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }


    private void updateImage(String imagePath, Uri uri) {
        if (null == imagePath) {
            return;
        }
      /*  AddImagsBean ab = new AddImagsBean();
        ab.setUrl(imagePath);
        EventBus.getDefault().post(ab);*/
        AllProjectAdapter.addImage(imagePath);
    }


}
